package com.treasure.service.creditCard.controller;

import com.treasure.service.annotation.LogInfo;
import com.treasure.service.creditCard.service.OrderService;
import com.treasure.service.domain.TApplyCardOrder;
import com.treasure.service.dto.TApplyCardOrderDto;
import com.treasure.service.enums.CodeEnum;
import com.treasure.service.result.PageResult;
import com.treasure.service.result.ResponseResult;
import com.treasure.service.third.utils.TokenConfig;
import com.treasure.service.third.utils.UserToken;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
* @Description:    订单
* @Author:         panxinwen
* @CreateDate:     2018/10/15 21:13
* @Version:        1.0
*/
@Api(value = "订单",description = "订单相关操作")
@RestController
@RequestMapping("/order/v1")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "获取信用卡申请链接", notes = "获取信用卡申请链接")
    @LogInfo(log = "创建信用卡订单",isPrint = true)
    @PostMapping("/createCardOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId", value = "银行编码", required = false, dataType = "String", paramType = "query")
    })
    public ResponseEntity<ResponseResult> createCardOrder(@RequestBody(required = false) TApplyCardOrderDto applyCardOrder) throws UnsupportedEncodingException {
    	UserToken user = TokenConfig.getTokenByHeader();
    	applyCardOrder.setRealName(user.getCardName());
    	applyCardOrder.setCertNo(user.getCardNumber());
    	applyCardOrder.setMobile(user.getPhonenumber());
    	applyCardOrder.setCustomerId(TokenConfig.getTokenByHeader().getUserId());
        ResponseResult responseResult =orderService.createApplyCardOrder(applyCardOrder);
        return  ResponseEntity.ok(responseResult);

    }

    @ApiOperation(value = "获取我的订单", notes = "获取我的订单")
    @LogInfo(log = "获取我的订单",isPrint = true)
    @PostMapping("/getCardOrderList")
    @ApiImplicitParams({})
    public ResponseEntity<ResponseResult> getCardOrderList(@RequestParam(value = "pageSize") Integer pageSize,
                                                                     @RequestParam(value = "pageNum") Integer pageNum,
                                                                     @RequestParam(value = "userId") Long userId) throws UnsupportedEncodingException {
        PageResult<List<TApplyCardOrderDto>> result = orderService.getCardOrderList(pageSize,pageNum,userId);

        return  ResponseEntity.ok(new ResponseResult(CodeEnum.SUCCESS,result));

    }
}
